package com.jinyi.wwdeng.ui.config.act

import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.view.Gravity
import android.view.View
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.jinyi.base.base.BaseActivity
import com.jinyi.base.utils.BarUtils
import com.jinyi.base.utils.HHLog
import com.jinyi.idcardcamera.weight.CameraView
import com.jinyi.idcardcamera.weight.MaskView
import com.jinyi.idcardcamera.weight.PermissionCallback
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.ActIdcardCameraBinding
import java.io.File

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/22 11:45
 * @Annotation：证件照拍摄
 */
class IdCardCameraAct : BaseActivity() {
    lateinit var binding: ActIdcardCameraBinding

    companion object {
        const val KEY_OUTPUT_FILE_PATH = "outputFilePath"
        const val PERMISSIONS_REQUEST_CAMERA = 800
    }

    private var outputFile: File? = null

    override fun getRootView(): View {
        binding = ActIdcardCameraBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        setTitleBarVisibility(View.VISIBLE)
        setTitleBarColor(ContextCompat.getColor(this,R.color.color_353535))
        setBackBtnRes(R.mipmap.pic_back_white_icon)
        setRightBtnRes(R.mipmap.pic_kefu_white_icon)
        setTextTitle(getString(R.string.confirmer_le_compte))
        setTextTitleColor(ContextCompat.getColor(this, com.jinyi.lib_base.R.color.color_ffffff))
        setTextTitleGravity(Gravity.CENTER)
        BarUtils.setStatusBarLightMode(this, false)

        binding.cameraView.cameraControl.setPermissionCallback(permissionCallback)
        binding.cameraView.maskView.setMaskColor(ContextCompat.getColor(this,R.color.color_353535))

        initParams()
    }

    override fun onStart() {
        super.onStart()
        binding.cameraView.start()
    }

    override fun onStop() {
        super.onStop()
        binding.cameraView.stop()
    }

    override fun initListener() {
        //拍照
        binding.takePhotoButton.setOnClickListener {
            binding.cameraView.takePicture(outputFile, takePictureCallback);
        }
    }

    override fun initData() {
    }

    /**
     * 图片剪裁
     */
    private val openCropResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
        when (result.resultCode) {
            RESULT_OK -> {
                HHLog.e("拍照后剪裁返回，需要再次打开相机")
                binding.cameraView.cameraControl.resume()
            }
        }
    }

    private val permissionCallback = PermissionCallback {
        ActivityCompat.requestPermissions(
            this, arrayOf(
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ), PERMISSIONS_REQUEST_CAMERA)
        false
    }

    private fun initParams() {
        val outputPath = intent.getStringExtra(KEY_OUTPUT_FILE_PATH)
        if (outputPath != null) {
            outputFile = File(outputPath)
        }
        binding.cameraView.setMaskType(MaskView.MASK_TYPE_ID_CARD_FRONT)
    }

    private val takePictureCallback = CameraView.OnTakePictureCallback { bitmap ->
        runOnUiThread {
            HHLog.e("照片路径>>>>>>>>>>>>>>${outputFile!!.absolutePath}")
            //刷新图库
            sendBroadcast(Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.fromFile(outputFile)))
            binding.cameraView.cameraControl.pause()

            val intent = Intent(this,IdCardCropAct::class.java)
            val bundle = Bundle()
            bundle.putInt(IdCardCropAct.KEY_CONTENT_TYPE,1)//1相机  2相册
            bundle.putString(IdCardCropAct.KEY_FILE_PATH,outputFile!!.absolutePath)
            intent.putExtras(bundle)
            openCropResultLauncher.launch(intent)
//            onIntent(IdCardCropAct::class.java,bundle)
//            finish()
        }
    }
}